package com.divit.springboot.application.util;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Set;

import com.divit.springboot.application.model.Candidate;
import java.io.IOException;
import java.net.*;

public final class CandidatesUtil {

	private CandidatesUtil() {
	}

	/**
	 *
	 * Method to help us get dummy data
	 *
	 * @return list of candidates
	 */
//	public static List<Candidate> getCandidates() {
//		var candidateList = List.of(
//				new Candidate("John", 2, Set.of("java", "c#", "c++", "golang")),
//				new Candidate("David", 1, Set.of("javascript", "c++")),
//				new Candidate("Diana", 4, Set.of("java", "python")),
//				new Candidate("June", 2, Set.of("ruby", "aws", "docker")),
//				new Candidate("April", 3, Set.of("python", "Erlang", "docker")));
//
//		return candidateList;
//	}

  public static void broadcast(int protocolCode) throws Exception {
    String sendString = String.format("{\"ProtocolCode\": %s,}", protocolCode);
    byte[] sendData = sendString.getBytes("UTF-8");
    DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, InetAddress.getByName("255.255.255.255"), 63003);
    DatagramSocket socket = new DatagramSocket(63003);
    socket.setBroadcast(true);
    socket.send(sendPacket);
    socket.close();
  }

//  public static String send(int protocolCode) throws Exception {
//    DatagramSocket receiveSocket = new DatagramSocket(63002);
//    byte[] receiveData = new byte[1024];
//    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
//    for(int i=0;i<10;i++){
//      System.out.println("RECEIVED: " + i);
//      receiveSocket.receive(receivePacket);
//      if(receivePacket.getData() !=null) {
//        String sentence = new String(receivePacket.getData(), 0, receivePacket.getLength());
//        receiveSocket.close();
//        return sentence;
//      }
//      broadcast(protocolCode);
//    }
//    receiveSocket.close();
//    return null;
//  }
}
